<template>
    <div>
        <div style="margin-bottom: 20px;">
            <el-input v-model="query.name" placeholder="请输入名称" style="width: 200px;margin-right: 20px;"></el-input>
            <el-button type="primary" @click="select">查询</el-button>
        </div>
        <div v-if="store.state.user.type == 3">
            <el-button type="primary" @click="add(null)">新增视频</el-button>
            <el-button type="danger" @click="handDelete" :disabled="!ids.length">批量删除</el-button>

        </div>
        <el-table border :data="list" style="width: 100%;margin-top: 20px;"  @selection-change="handleSelectionChange">
            <el-table-column
            type="selection"
            width="55">
          </el-table-column>
            <el-table-column prop="name" label="视频名称" />
            <el-table-column prop="cname" label="分类名称" />
            <el-table-column prop="ding" label="顶次数" />
            <el-table-column prop="cai" label="踩次数" />
            <el-table-column prop="img" label="封面图">
    <template #default="scope">
       <img :src="scope.row.img" alt="" v-if="scope.row.img" style="width: 100px;height: 100px;">
       <span v-else>暂无头像</span>
    </template>
</el-table-column>
<el-table-column label="操作"  width="260px">
    <template #default="scope">
                    <!-- {{scope.row}} -->
                    <el-button type="text" @click="add(scope.row)">修改</el-button>
                    <el-button type="text" @click="chakan(scope.row)">查看视频</el-button>
                    <el-button type="text" @click="timu(scope.row)">题目</el-button>
                    <el-button type="text" @click="handDelete(scope.row)">删除</el-button>
                </template>
</el-table-column>
<!-- <el-table-column prop="address" label="Address" /> -->
</el-table>
<el-pagination v-show="total>0" style="margin-top: 20px;" :currentPage="query.page" :page-sizes="[100, 200, 300, 400]" background layout="total,  prev, pager, next, jumper" :page-size="query.size" :total="total" @current-change="currentChange"></el-pagination>
<el-dialog destroy-on-close v-model="dialogVisible" :title="type=='add'?'新增视频':'修改视频'" width="500px" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="formRef" :inline="false" size="default" label-width="100px">
        <el-form-item prop="name" style="width: 100%;" label="视频名称">
            <el-input v-model="form.name" placeholder="请输入视频名称" style="width: 100%;">
            </el-input>
        </el-form-item>
        <el-form-item prop="cid" style="width: 100%;"  label="视频分类">
            <el-select v-model="form.cid" placeholder="请选择分类" style="width:100%">
                <el-option :label="v.name" :value="v.id" v-for="v in cList" :key="v.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="url" style="width: 100%;"  label="视频">
            <el-upload
  class="upload-demo"  :before-remove="beforeRemove"
  action="/api/uploadVideo" :on-success="handleAvatarSuccess"
  :limit="1"
    :before-upload="beforeAvatarUpload"
  :file-list="fileList">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传mp4文件</div>
</el-upload>
        </el-form-item>
        <el-form-item  label="封面图" prop="img" >
            <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleAvatarSuccess2" :before-upload="beforeAvatarUpload2">
                <img v-if="form.img" :src="form.img" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>
        </el-form-item>
    </el-form>
    <template #footer>
  <span class="dialog-footer">
    <el-button @click="dialogVisible = false">取消</el-button>
    <el-button type="primary" @click="submit">
      确定
    </el-button>
  </span>
</template>
</el-dialog>
<el-dialog destroy-on-close v-model="dialogVisible2" title="查看视频" width="800px" :before-close="handleClose">
   
   <video controls :src="videoUrl" style="width: 800px;height: 400px;"></video>
</el-dialog>
</div>
</template>
<script setup>
 import {
        useRouter
    } from 'vue-router'
    import {
        onMounted,
        ref,
        reactive
    } from 'vue'
    import {deleteVideo,
        shipinfenleiList,
        deptAdd,addmvideo,
        deptDelete,
        shipinfenleideletee,getvideoList,
        shipinfenleiUpdate,videoUpdate,
        regshipinfenlei

    } from "~/api/xm.js"
    import {
        useStore
    } from 'vuex'
    import {
        ElMessage,
        ElMessageBox
    } from 'element-plus'
    import {
        Plus
    } from '@element-plus/icons-vue'
    const store = useStore()
    const ids = ref([])
    const dialogVisible2=ref(false)
    const videoUrl=ref(null)
    const fileList=ref([])
    const cList = ref([])
    const router = useRouter()
    const list = ref([])
    const total = ref(0)
    const type = ref('')
    const id = ref('')
    const dialogVisible = ref(false)
    const query = ref({
        page: 1,
        size: 10,
        name: '',
       
    })
    const form = reactive({
        name: "",
        url: '',
        cid:'',
        img: '',
    })
    function handleAvatarSuccess2(e) {
        form.img = e.data
        console.log(e);
    }
    function chakan(row){
        dialogVisible2.value=true
        
        videoUrl.value=row.url
    }
    function handleAvatarSuccess(e) {
        form.url = e.data
        console.log(e);
    }
    function timu(row){
        router.push("/timu?vid="+row.id)
    }
function  beforeAvatarUpload(file) {
    console.log(file);
        const isJPG = file.type === 'video/mp4';
        if (!isJPG) {
            ElMessage({
                                type: 'warning',
                                message: '上传类型只能是 mp4格式!',
                            })
        }
        return isJPG;
      }
      function  beforeRemove(file, fileList) {
        form.url = ''
        console.log(file);
      }
function  beforeAvatarUpload2(file) {
    console.log(file);
        const isJPG = file.type.indexOf('image')!=-1;
        if (!isJPG) {
            ElMessage({
                                type: 'warning',
                                message: '上传类型只能是 图片格式!',
                            })
        }
        return isJPG;
      }
    function currentChange(e) {
        query.value.page = e
        select()
        console.log(e);
    }

    function handleSelectionChange(e) {

        ids.value = e.map(v => v.id)
        console.log(ids.value);
    }
    const rules = {
        name: [{
            required: true,
            message: '名称不能为空',
            trigger: 'change',
        }],
        cid: [{
            required: true,
            message: '分类不能为空',
            trigger: 'change',
        }],
        img: [{
            required: true,
            message: '封面图不能为空',
            trigger: 'change',
        }],
        url: [{
            required: true,
            message: '视频不能为空',
            trigger: 'change',
        }]
    }
    onMounted(() => {
        getCidList()
        select()
    })

    function handDelete(row) {
        let id = row.id ? row.id : ids.value.join(',')
        console.log(id);
        ElMessageBox.confirm(
                `是否删除id为${id}的数据?`,
                'Warning', {
                    confirmButtonText: '确定删除',
                    cancelButtonText: '取消删除',
                    type: 'warning',
                }
            )
            .then(() => {
                deleteVideo(id).then(res => {
                    if (res.code == 200) {
                        ElMessage({
                            type: 'success',
                            message: 'Delete completed',
                        })
                        select()
                    }
                })

            })

    }

    function add(e) {
        form.id = ''
        form.name = ''
        form.cid = ''
        form.img = ''
        form.url = ''
        type.value = "add"
        dialogVisible.value = true
        fileList.value=[]
        if (e && e.id) {
            type.value = "edit"
            form.id = e.id
            form.name = e.name
               form.cid = e.cid
        form.img = e.img
        form.url = e.url
        fileList.value=[{url:e.url,name:e.name}]
        }
    }
    function getCidList() {
        shipinfenleiList({page:1,size:99}).then(res => {
            cList.value = res.data.list
        })
    }
    function select() {
        getvideoList(query.value).then(res => {
            total.value = res.data.total
            list.value = res.data.list
            console.log(res);
        })
    }
    const formRef = ref(null)
    const rowId = ref('')

    function submit() {
        formRef.value.validate((valid) => {
            if (valid) {
                if (type.value == "add") {
                    addmvideo(form).then(res => {
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '新增成功',
                            })
                            dialogVisible.value = false
                            select()
                        }

                    })
                } else {
                    videoUpdate(form).then(res => {
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '修改成功',
                            })
                            dialogVisible.value = false
                            select()
                        }
                    })
                }


            }
        })
    }
</script>
<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }
    
    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }
    
    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>